var language_type = localStorage.getItem('language_type');
var maNum; // 验证码所剩的时间
$(document).ready(function () {
 if (language_type == 2) {
  $("#get_yzm").attr("value", "Verification Code") // 如果是英文，替换value值
 }
 language();
 documentName()
})
// 验证码倒计时
function settime(num) {
 var yzm_input = document.getElementById("get_yzm");
 var num;
 if (num == 0) {
  regcode = true;
  // console.log(language_type == 2)
  yzm_input.value = language_type == 1 ? "重新发送" : "Resend"
  yzm_input.removeAttribute("disabled");
  return false; //直到倒计时0时停止执行函数
 } else {
  yzm_input.setAttribute("disabled", "disabled");
  if (language_type == 1) {
   yzm_input.value = num + "s后重发";
  } else {
   yzm_input.value = "Resend after " + num + "s";
  }
  num--;
  maNum = num;
 }
 setTimeout("settime(" + num + ")", 1000);
}
$("#get_yzm").click(function () {
 let m_phone = $("#reg_phone").val(); // 手机号
  let req1 = {
   type: 1,
   mobile: m_phone
  }
  $.ajax({
   type: 'post',
   url: baseURL + '/index.php?m=login&c=sms_code',
   data: req1,
   success: function success(data) {
    data = JSON.parse(data);
    if (data.code == 1) {
     settime(60)
     layer.open({
      content: language_type == 1 ? '发送成功' : 'Sent successfully',
      time: 1
     });
    } else {
     layer.open({
      content: data.msg,
      time: 1
     });
    }
   }
  });
})

$("#reg_btn").click(function () {
 // location.href = "login.html" // 跳转登录页
 let m_phone = $("#reg_phone").val(); // 手机号
 let m_code = $("#reg_yzm").val(); // 验证码
 let m_nickname = $("#reg_all_name").val(); // 全名
 let m_account = $("#reg_user_name").val(); // 账号名称
 let m_email = $("#reg_email").val(); // 电邮
 let m_login_pwd1 = $("#reg_pass").val(); // 密码
 let w_login_pwd2 = $("#reg_sure_pass").val(); // 确认密码
 let m_pay_pwd1 = $("#reg_jiaoyi_pass").val(); // 交易密码
 let m_pay_pwd2 = $("#reg_sure_jiaoyi_pass").val(); // 确认交易密码
 let m_invite_code = $("#reg_yao_ma").val(); // 邀请码
 var req2 = {
  m_invite_code: m_invite_code,
  language_type: language_type,
  m_nickname: m_nickname,
  m_phone: m_phone,
  m_code: m_code,
  m_login_pwd1: m_login_pwd1,
  w_login_pwd2: w_login_pwd2,
  m_pay_pwd1: m_pay_pwd1,
  m_pay_pwd2: m_pay_pwd2,
  m_account: m_account,
  m_email: m_email,
 };
 if (m_code != '123456') {
  layer.open({
   content: language_type == 1 ? '验证码错误' : 'Verification code error',
   time: 1
  })
 } else if (!m_nickname) {
  layer.open({
   content: language_type == 1 ? '请填写您的全名' : 'Please fill in your full name',
   time: 1
  })
 } else if (!m_account) {
  layer.open({
   content: language_type == 1 ? '请填写您的账号名称' : 'Please fill in your account name',
   time: 1
  })
 } else if (!/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(m_email)) {
  layer.open({
   content: language_type == 1 ? '邮箱格式不正确' : 'Invalid email format ',
   time: 1
  })
 } else if (!/^[\w]{6,12}$/.test(m_login_pwd1)) {
  layer.open({
   content: language_type == 1 ? '请输入6-12位有效密码' : 'Please key in 6-12 characters',
   time: 1
  })
 } else if (w_login_pwd2 != m_login_pwd1) {
  layer.open({
   content: language_type == 1 ? '两次密码输入不一致' : 'Password did not match, Please try again',
   time: 1
  })
 } else if (!/^[\w]{6,12}$/.test(m_pay_pwd1)) {
  layer.open({
   content: language_type == 1 ? '请输入6-12位有效交易密码' : 'Please key in 6-12 characters',
   time: 1
  })
 } else if (m_pay_pwd1 != m_pay_pwd2) {
  layer.open({
   content: language_type == 1 ? '两次交易密码输入不一致' : 'Different input password',
   time: 1
  })
 } else if (!m_invite_code) {
  layer.open({
   content: language_type == 1 ? '请填写邀请码' : 'Please fill in the invitation code',
   time: 1
  })
 } else if (maNum == 0) {
  layer.open({
   content: language_type == 1 ? '验证码已失效,请重新发送' : 'Verification code has expired, please resend',
   time: 1
  })
 } else {
  $.ajax({
   type: 'post',
   url: baseURL + '/index.php?m=login&c=register',
   data: req2,
   success: function success(data) {
    data = JSON.parse(data);
    console.log(data.code == 1)
    if(data.code == 1){
     layer.open({
      content: data.msg,
      time: 1
     });
     setTimeout(function(){
      window.location.href = 'login.html'
     }, 1000);
    } else {
     layer.open({
      content: data.msg,
      time: 1
     });
    }
   }
  });
 }
})
